home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / ilisp / ilisp-snd.el.z / ilisp-snd.el
Encoding:
Text File  |  1998-05-21  |  15.8 KB  |  514 lines

  1. ;;; -*- Mode: Emacs-Lisp -*-
  2.  
  3. ;;; ilisp-snd.el --
  4.  
  5. ;;; This file is part of ILISP.
  6. ;;; Version: 5.8
  7. ;;;
  8. ;;; Copyright (C) 1990, 1991, 1992, 1993 Chris McConnell
  9. ;;;               1993, 1994 Ivan Vasquez
  10. ;;;               1994, 1995, 1996 Marco Antoniotti and Rick Busdiecker
  11. ;;;               1996 Marco Antoniotti and Rick Campbell
  12. ;;;
  13. ;;; Other authors' names for which this Copyright notice also holds
  14. ;;; may appear later in this file.
  15. ;;;
  16. ;;; Send mail to 'ilisp-request@naggum.no' to be included in the
  17. ;;; ILISP mailing list. 'ilisp@naggum.no' is the general ILISP
  18. ;;; mailing list were bugs and improvements are discussed.
  19. ;;;
  20. ;;; ILISP is freely redistributable under the terms found in the file
  21. ;;; COPYING.
  22.  
  23.  
  24.  
  25. ;;;
  26. ;;; ILISP send and support.
  27. ;;;
  28.  
  29.  
  30. ;;;%% Package / Symbol support
  31. ;;;
  32. (defun lisp-buffer-package ()
  33.   "Return the package for this buffer.  The package name is a string.
  34. If there is none, return NIL.  This caches the package unless
  35. ilisp-dont-cache-package is non-nil, so calling this more than once
  36. is cheap."
  37.   (cond ((and (not (eq buffer-package 'not-yet-computed))
  38.           (null lisp-dont-cache-package)) 
  39.      buffer-package)
  40.     (ilisp-completion-package ilisp-completion-package)
  41.     (lisp-dont-cache-package
  42.      ;; Refind the package each time.
  43.      (let ((package (lisp-buffer-package-internal nil)))
  44.        (message "")
  45.        (setq buffer-package 'not-yet-computed)
  46.        (if package
  47.            (setq mode-name
  48.              (concat 
  49.               (or buffer-mode-name 
  50.               (setq buffer-mode-name mode-name))
  51.               ":" package)))
  52.        package))
  53.     ((or lisp-buffer-package 
  54.          (memq major-mode ilisp-modes)
  55.          (not (memq major-mode lisp-source-modes)))
  56.      nil)
  57.     (t
  58.      (make-local-variable 'buffer-package)
  59.      (make-local-variable 'buffer-mode-name)
  60.      (let ((package (lisp-buffer-package-internal t)))
  61.        (message "")
  62.        (setq buffer-package package)
  63.        ;; Display package in mode line
  64.        (if package 
  65.            (setq mode-name
  66.              (concat (or buffer-mode-name
  67.                  (setq buffer-mode-name mode-name))
  68.                  ":" buffer-package)))
  69.        buffer-package))))
  70.  
  71. (defun lisp-buffer-package-internal (search-from-start)
  72.   "Returns the package of the buffer.  If SEARCH-FROM-START is T then
  73. will search from the beginning of the buffer, otherwise will search
  74. backwards from current point."
  75.   (setq mode-line-process 'ilisp-status)
  76.   (let* ((lisp-buffer-package t)
  77.      (case-fold-search t)
  78.      (regexp (ilisp-value 'ilisp-package-regexp t))
  79.      (spec
  80.       (if regexp
  81.           (save-excursion
  82.         (if (or (and search-from-start
  83.                  (goto-char (point-min))
  84.                  (re-search-forward regexp nil t))
  85.             (re-search-backward regexp nil t))
  86.             (buffer-substring (match-beginning 0)
  87.                       (progn 
  88.                     (goto-char (match-beginning 0))
  89.                     (forward-sexp)
  90.                     (point)))))))
  91.      (str  (format (ilisp-value 'ilisp-package-command) spec))
  92.      (package
  93.       (if spec
  94.           (ilisp-send 
  95.            str
  96.            "Finding buffer package"
  97.            'pkg))))
  98.     (if (ilisp-value 'comint-errorp t)
  99.     (progn
  100.       (lisp-display-output package)
  101.       (error "No package"))
  102.     (if (and package 
  103.          ;; There was a bug here, used to have the second *
  104.          ;; outside of the parens.
  105.          (string-match "[ \n\t:\"]*\\([^ \n\t\"]*\\)" package))
  106.         (setq package
  107.           (substring package
  108.                  (match-beginning 1) (match-end 1)))))
  109.     package))
  110.  
  111. ;;;
  112. (defun package-lisp ()
  113.   "Show current inferior LISP package."
  114.   (interactive)
  115.   (message "Inferior LISP package is %s"
  116.        (ilisp-send (ilisp-value 'ilisp-package-name-command)
  117.                "Finding inferior LISP package" 'pkg)))
  118.  
  119. ;;;
  120. (defun set-package-lisp (package)
  121.   "Set inferior LISP to package of buffer or a named package with prefix."
  122.   (interactive 
  123.    (let ((default (lisp-buffer-package)))
  124.      (if (or current-prefix-arg (null default))
  125.      (let ((name
  126.         (read-string
  127.          (format "Package [%s]: " (lisp-buffer-package)) "")))
  128.        (list (if (equal name "") default name)))
  129.      (list default))))
  130.   (if package
  131.       (ilisp-send (format (ilisp-value 'ilisp-in-package-command) package)
  132.           (format "Set %s's package to %s" 
  133.               (buffer-name (ilisp-buffer))
  134.               package)
  135.           'pkg 'dispatch)
  136.       (error "No package")))
  137.  
  138. ;;;
  139. (defun set-buffer-package-lisp (package)
  140.   "Reset the current package of the current buffer.  With prefix
  141. specify manually."
  142.   (interactive (if current-prefix-arg
  143.            (list (read-from-minibuffer "Package: " ))
  144.            (list nil)))
  145.   (if package
  146.       (setq buffer-package package
  147.         mode-name (concat (or buffer-mode-name mode-name) ":" package))
  148.       (setq buffer-package 'not-yet-computed)
  149.       (lisp-buffer-package)))
  150.  
  151.  
  152.  
  153. ;;;%Interface functions
  154. ;;;%%Symbols
  155. (defun lisp-string-to-symbol (string)
  156.   "Convert STRING to a symbol, (package delimiter symbol) where the
  157. package is either package:symbol or from the current buffer."
  158.   (let* ((start (string-match ":+" string))
  159.      (end (if start (match-end 0))))
  160.     (if start
  161.     (lisp-symbol
  162.      (if (= start 0)
  163.          ""
  164.          (substring string 0 start))
  165.      (substring string start end)
  166.      (substring string end))
  167.     (let ((package (lisp-buffer-package)))
  168.       (lisp-symbol package (if package "::") string)))))
  169.  
  170. ;;;
  171. (defun lisp-symbol-to-string (symbol)
  172.   "Convert SYMBOL to a string."
  173.   (apply 'concat symbol))
  174.  
  175. ;;;
  176. (defun lisp-buffer-symbol (symbol)
  177.   "Return SYMBOL as a string qualified for the current buffer."
  178.   (let ((symbol-name (lisp-symbol-name symbol))
  179.     (pkg (lisp-symbol-package symbol))
  180.     (delimiter (lisp-symbol-delimiter symbol)))
  181.     (cond ((string= pkg (lisp-buffer-package)) symbol-name)
  182.       ((string= pkg "") (concat ":" symbol-name))
  183.       (pkg (concat pkg delimiter symbol-name))
  184.       (t symbol-name))))
  185.  
  186. ;;;
  187. (defun lisp-previous-symbol (&optional stay)
  188.   "Return the immediately preceding symbol as ((package delimiter symbol)
  189. function-p start end).  If STAY is T, the end of the symbol will be point."
  190.   (save-excursion
  191.     (if (or (and (memq major-mode ilisp-modes)
  192.          (= (point) (process-mark (get-buffer-process
  193.                        (current-buffer)))))
  194.         (progn
  195.           (skip-chars-backward " \t\n")
  196.           (or (bobp) (memq (char-after (1- (point))) '(?\) ?\")))))
  197.     nil
  198.     (let* ((delimiters (ilisp-value 'ilisp-symbol-delimiters))
  199.            (end (progn
  200.               (if (not stay) (skip-chars-forward delimiters))
  201.               (point)))
  202.            (start (progn
  203.             (skip-chars-backward delimiters)
  204.             (point)))
  205.            (prefix (if (not (bobp)) (1- start)))
  206.            (function-p
  207.         (and prefix
  208.              (or (eq (char-after prefix) ?\()
  209.              (and (eq (char-after prefix) ?')
  210.                   (not (bobp))
  211.                   (eq (char-after (1- prefix)) ?#)))
  212.              (not (looking-at "[^: \t\n]*:*\\*[^ \t\n]")))))
  213.       (cons (lisp-string-to-symbol (buffer-substring start end))
  214.         (list function-p start end))))))
  215.  
  216.  
  217. ;;;
  218. (defun lisp-function-name ()
  219.   "Return the previous function symbol.  This is either after a #' or
  220. at the start of the current sexp.  If there is no current sexp, return
  221. nil."
  222.   (save-excursion
  223.     (let ((symbol (lisp-previous-symbol)))
  224.       (if (car (cdr symbol))
  225.       (car symbol)
  226.       (condition-case ()
  227.           (if (and (memq major-mode ilisp-modes)
  228.                (= (point)
  229.               (process-mark 
  230.                (get-buffer-process (current-buffer)))))
  231.           nil
  232.           (backward-up-list 1)
  233.           (down-list 1)
  234.           (lisp-string-to-symbol
  235.            (buffer-substring (point) 
  236.                      (progn (forward-sexp 1) (point)))))
  237.         (error nil))))))
  238.  
  239.  
  240. ;;;
  241. (defun lisp-defun-name ()
  242.   "Return the name of the current defun."
  243.   (save-excursion
  244.     (lisp-defun-begin)
  245.     (lisp-string-to-symbol (lisp-def-name t))))
  246.  
  247.  
  248. ;;;%% ILISP initializations
  249. ;;;
  250. (defun ilisp-initialized ()
  251.   "Return T if the current inferior LISP has been initialized."
  252.   (memq (buffer-name (ilisp-buffer)) ilisp-initialized))
  253.  
  254. ;;;
  255. (defun ilisp-load-init (dialect file)
  256.   "Add FILE to the files to be loaded into the inferior LISP when
  257. dialect is initialized.  If FILE is NIL, the entry will be removed."
  258.   (let ((old (assoc dialect ilisp-load-inits)))
  259.     (if file
  260.     (if old
  261.         (rplacd old file)
  262.         (setq ilisp-load-inits (nconc ilisp-load-inits 
  263.                       (list (cons dialect file)))))
  264.     (if old (setq ilisp-load-inits (delq old ilisp-load-inits))))))
  265.  
  266. ;;;
  267. (defun ilisp-binary (init var)
  268.   "Initialize VAR to the result of INIT if VAR is NIL."
  269.   (if (not (ilisp-value var t))
  270.       (let ((binary (ilisp-value init t)))
  271.     (if binary
  272.         (comint-send 
  273.          (ilisp-process) binary
  274.          t nil 'binary nil 
  275.          (` (lambda (error wait message output last)
  276.           (if (or error
  277.               (not (string-match "\"[^\"]*\"" output)))
  278.               (progn
  279.             (lisp-display-output output)
  280.             (abort-commands-lisp "No binary"))
  281.               (setq (, var)
  282.                 (substring output
  283.                        (1+ (match-beginning 0))
  284.                        (1- (match-end 0))))))))))))
  285.  
  286. ;;;
  287. (defun ilisp-done-init ()
  288.   "Make sure that initialization is done and if not dispatch another check."
  289.   (if ilisp-load-files
  290.       (comint-send-code (get-buffer-process (current-buffer))
  291.             'ilisp-done-init)
  292.       (if ilisp-initializing
  293.       (progn
  294.         (message "Finished initializing %s" (car ilisp-dialect))
  295.         (setq ilisp-initializing nil
  296.           ilisp-initialized
  297.           (cons (buffer-name (current-buffer)) ilisp-initialized))))))
  298.  
  299. ;;;
  300. (defun ilisp-init-internal (&optional sync)
  301.   "Send all of the stuff necessary to initialize."
  302.   (unwind-protect
  303.        (progn
  304.      (if sync
  305.          (comint-sync
  306.           (ilisp-process)
  307.           "\"Start sync\""  "[ \t\n]*\"Start sync\""
  308.           "\"End sync\""    "\"End sync\""))
  309.      (ilisp-binary 'ilisp-binary-command 'ilisp-binary-extension)
  310.      (ilisp-binary 'ilisp-init-binary-command 
  311.                'ilisp-init-binary-extension)
  312.      ;; This gets executed in the process buffer
  313.      (comint-send-code
  314.       (ilisp-process)
  315.       (function (lambda ()
  316.         (let ((files ilisp-load-inits)
  317.           (done nil))
  318.           (unwind-protect
  319.            (progn
  320.              (if (not ilisp-init-binary-extension)
  321.              (setq ilisp-init-binary-extension 
  322.                    ilisp-binary-extension))
  323.              (while files
  324.                (ilisp-load-or-send
  325.             (expand-file-name 
  326.              (cdr (car files)) ilisp-directory))
  327.                (setq files (cdr files)))
  328.              (comint-send-code (ilisp-process)
  329.                        'ilisp-done-init)
  330.              (setq done t))
  331.         (if (not done)
  332.             (progn
  333.               (setq ilisp-initializing nil)
  334.               (abort-commands-lisp))))))))
  335.  
  336.      (set-ilisp-value 'ilisp-initializing t)) ; progn
  337.  
  338.     (if (not (ilisp-value 'ilisp-initializing t))
  339.     (abort-commands-lisp))))
  340.  
  341. ;;;
  342. (defun ilisp-init (&optional waitp forcep sync)
  343.   "Initialize the current inferior LISP if necessary by loading the
  344. files in ilisp-load-inits.  Optional WAITP waits for initialization to
  345. finish.  When called interactively, force reinitialization.  With a
  346. prefix, get the binary extensions again."  
  347.   (interactive 
  348.    (list (if current-prefix-arg
  349.          (progn
  350.            (set-ilisp-value 'ilisp-init-binary-extension nil)
  351.            (set-ilisp-value 'ilisp-binary-extension nil)
  352.            nil))
  353.      t))
  354.   (if (or forcep (not (ilisp-initialized)))
  355.       (progn
  356.     (message "Started initializing ILISP")
  357.     (if (not ilisp-directory)
  358.         (setq ilisp-directory (or (ilisp-directory "ilisp.elc" load-path)
  359.                       (ilisp-directory "ilisp.el" load-path))))
  360.     (if (not (ilisp-value 'ilisp-initializing t))
  361.         (ilisp-init-internal sync))
  362.     (if waitp
  363.         (while (ilisp-value 'ilisp-initializing t)
  364.           (accept-process-output)
  365.           (sit-for 0))))))
  366.  
  367. ;;;
  368. (defun ilisp-init-and-sync ()
  369.   "Synchronize with the inferior LISP and then initialize."
  370.   (ilisp-init nil nil t))
  371.  
  372.  
  373.  
  374. ;;;
  375. (defun call-defun-lisp (arg)
  376.   "Put a call of the current defun in the inferior LISP and go there.
  377. If it is a \(def* name form, look up reasonable forms of name in the
  378. input history unless called with prefix ARG. If not found, use \(name
  379. or *name* as the call.  If is not a def* form, put the whole form in
  380. the buffer."
  381.   (interactive "P")
  382.   (if (save-excursion (lisp-defun-begin) (looking-at "(def"))
  383.       (let* ((symbol (lisp-defun-name))
  384.          (name (lisp-symbol-name symbol))
  385.          (package (if (lisp-symbol-package symbol)
  386.               (concat "\\("
  387.                   (lisp-symbol-package symbol) ":+\\)?")))
  388.          (variablep (string-match "^\\*" name))
  389.          (setfp (string-match "(setf \\([^\)]+\\)" name)))
  390.     (switch-to-lisp t t)
  391.     (cond (setfp 
  392.            (setq name 
  393.              (substring name (match-beginning 1) (match-end 1)))
  394.            (lisp-match-ring (if (not arg)
  395.                     (concat "(setf[ \t\n]*(" 
  396.                         package name "[ \t\n]"))
  397.                 (concat "(setf (" name)))
  398.           (variablep (lisp-match-ring (if (not arg) 
  399.                           (concat package name))
  400.                       name))
  401.           (t
  402.            (let ((fun (concat "(" name)))
  403.          (setq name (regexp-quote name))
  404.          (or (lisp-match-ring 
  405.               (if (not arg) (concat "(" package name "[ \t\n\)]"))
  406.               fun 
  407.               (not arg))
  408.              (lisp-match-ring (concat "(" package
  409.                           "[^ \t\n]*-*" name)
  410.                       fun))))))
  411.     (let ((form 
  412.        (save-excursion
  413.          (buffer-substring (lisp-defun-begin) 
  414.                    (lisp-end-defun-text t)))))
  415.       (switch-to-lisp t t)
  416.       (comint-kill-input)
  417.       (insert form))))
  418.  
  419.  
  420.  
  421. ;;;
  422. (defun ilisp-send (string &optional message status and-go handler)
  423.   "Send STRING to the ILISP buffer, print MESSAGE set STATUS and
  424. return the result if AND-GO is NIL, otherwise switch to ilisp if
  425. and-go is T and show message and results.  If AND-GO is 'dispatch,
  426. then the command will be executed without waiting for results.  If
  427. AND-GO is 'call, then a call will be generated. If this is the first
  428. time an ilisp command has been executed, the lisp will also be
  429. initialized from the files in ilisp-load-inits.  If there is an error,
  430. comint-errorp will be T and it will be handled by HANDLER."
  431.   (ilisp-init t)
  432.   (let ((process (ilisp-process))
  433.     (dispatch (eq and-go 'dispatch)))
  434.     (if message
  435.     (message "%s" (if dispatch
  436.               (concat "Started " message)
  437.               message)))
  438.     ;; No completion table
  439.     (setq ilisp-original nil)
  440.     (if (memq and-go '(t call))
  441.     (progn (comint-send process string nil nil status message handler)
  442.            (if (eq and-go 'call)
  443.            (call-defun-lisp nil)
  444.            (switch-to-lisp t t))
  445.            nil)
  446.     (let* ((save (ilisp-value 'ilisp-save-command t))
  447.            (result
  448.         (comint-send 
  449.          process
  450.          (if save (format save string) string)
  451.          ;; Interrupt without waiting
  452.          t (if (not dispatch) 'wait) status message handler)))
  453.       (if save 
  454.           (comint-send
  455.            process
  456.            (ilisp-value 'ilisp-restore-command t)
  457.            t nil 'restore "Restore" t t))
  458.       (if (not dispatch)
  459.           (progn
  460.         (while (not (cdr result))
  461.           (sit-for 0)
  462.           (accept-process-output))
  463.         (comint-remove-whitespace (car result))))))))
  464.  
  465.  
  466.  
  467. ;;;
  468. (defun ilisp-load-or-send (file)
  469.   "Try to load FILE into the inferior LISP.  If the file is not
  470. accessible in the inferior LISP as determined by
  471. ilisp-load-or-send-command, then visit the file and send the file over
  472. the process interface."
  473.   (let* ((command
  474.       (format (ilisp-value 'ilisp-load-or-send-command) 
  475.           (lisp-file-extension
  476.            file 
  477.            (ilisp-value 'ilisp-init-binary-extension t))
  478.           file)))
  479.     (set-ilisp-value 'ilisp-load-files 
  480.              (nconc (ilisp-value 'ilisp-load-files t) (list file)))
  481.     (comint-send
  482.      (ilisp-process) command t nil 'load
  483.      (format "Loading %s" file)
  484.      (function (lambda (error wait message output last)
  485.        (let* ((file (lisp-last ilisp-load-files))
  486.           (process (get-buffer-process (current-buffer)))
  487.           (case-fold-search t))
  488.      (if (and output 
  489.           (string-match "nil" (car (lisp-last-line output))))
  490.          (let* ((old-buffer (get-file-buffer file))
  491.             (buffer (find-file-noselect file))
  492.             (string (save-excursion
  493.                   (set-buffer buffer)
  494.                   (buffer-string))))
  495.            (if (not old-buffer) (kill-buffer buffer))
  496.            (if (string= "" string)
  497.            (abort-commands-lisp (format "Can't find file %s" file))
  498.            (comint-send
  499.             process
  500.             (format ilisp-block-command string)
  501.             t nil 'send (format "Sending %s" file)
  502.             (function (lambda (error wait message output last)
  503.               (if error
  504.               (progn 
  505.                 (comint-display-error output)
  506.                 (abort-commands-lisp
  507.                  (format "Error sending %s"
  508.                      (lisp-last ilisp-load-files))))
  509.               (setq ilisp-load-files
  510.                 (delq (lisp-last ilisp-load-files)
  511.                       ilisp-load-files))))))))
  512.            (if error (ilisp-handler error wait message output last))
  513.            (setq ilisp-load-files (delq file ilisp-load-files)))))))))
  514.